<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ include file="core.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>供货商管理</title>
    <script type="text/javascript" src="${pageContext.request.contextPath }/static/js/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/bootstrap.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/My97DatePicker/WdatePicker.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/plugins/bootstrap-table/bootstrap-table.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/static/js/plugins/bootstrap-table/bootstrap-table-zh-CN.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/plugins/bootstrap-validator/bootstrapValidator.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/static/js/plugins/bootstrap-validator/zh_CN.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/static/js/plugins/bootstrap-table/export/bootstrap-table-export.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/static/layer/layer.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/static/js/plugins/select2/select2.full.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/static/js/plugins/treeview/bootstrap-treeview.js"></script>
    <link href="${pageContext.request.contextPath}/static/hplus/css/bootstrap.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/hplus/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/hplus/css/font-awesome.min93e3.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/hplus/css/animate.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/hplus/css/style.min862f.css" rel="stylesheet">
    <script>

        /**
         * 用户
         */
        $(function (){
            getDeptLists();

            //select2 多选
            $("#rid").select2({
                //minimumInputLength: 1,
                language: "zh-CN", //设置 提示语言
                maximumSelectionLength: 3,  //设置最多可以选择多少项
                //width: "100%", //设置下拉框的宽度
                placeholder: "请选择",
                tags: true,
            });


            $("#test-table").bootstrapTable('destroy');
            $('#test-table').bootstrapTable({
                method : 'GET', //默认是post,不允许对静态文件访问
                url: "/gong/userList.action",
                cache : false,
                striped : true,// 隔行加亮
                pagination : true, //开启分页功能    在表格底部显示分页工具栏
                pageSize : 5, //默认每页条数
                pageNumber : 1, //默认分页
                pageList : [ 10, 20, 50, 100, 200, 500 ],//分页数
                showColumns : true, //显示隐藏列
                showRefresh : false, //显示刷新按钮
                toolbar:"#toolbar",
                singleselect : true,
                minimumCountColumns: 2,// 设置最少显示列个数
                clickToSelect: true, // 单击行即可以选中
                search : false,//显示搜素表单
                silent : true, //刷新事件必须设置
                sidePagination : "server", //表示服务端请求
                columns : [ {
                    checkbox:true
                }   ,{
                    field : "ghid",
                    title : "供货商编号",
                    class : 'col-md-1',
                    align : "center",
                    valign : "middle",
                    sortable : "true"
                }, {
                    field : "ghuo",
                    title : "供货商名称",
                    align : "center",
                    valign : "middle",
                    sortable : "true"
                }, {
                    field : "ghxm",
                    title : "联系人",
                    align : "center",
                    valign : "middle",
                    sortable : "true"
                },  {
                    field : "ghphone",
                    title : "电话",
                    align : "center",
                    valign : "middle",
                    sortable : "true"
                },{
                    field : "ghdizhi",
                    title : "地址",
                    align : "center",
                    valign : "middle",
                    sortable : "true"
                },
                    {
                        field : "ghbeizhu",
                        title : "备注",
                        align : "center",
                        valign : "middle",
                        sortable : "true"
                    },{
                        field: 'operate',
                        title: '操作',
                        class : 'col-md-2',
                        align: 'center',
                        valign: 'middle',
                        formatter: operateFormatter,
                    }],
                queryParamsType: "undefined",
                queryParams: function queryParams(params) {   //设置查询参数
                    var param = {
                        pageNumber: params.pageNumber,
                        pageSize: params.pageSize,
                        ghid:$("#ghid").val(),
                        ghxm:$("#ghxm").val(),
                        deptId:$("#deptId").val()
                        // searchText: params.searchText
                    };
                    return param;
                },
                formatLoadingMessage : function() {
                    return "请稍等，正在加载中...";
                },

                formatNoMatches : function() {
                    return '无符合条件的记录';
                }
            });
        });

        function operateFormatter(value, row, index) {
            return [
                '&nbsp;&nbsp;&nbsp;<button class=" btn btn-danger" type="button" onclick="delUser('+row.ghid+')">删除</button>'
            ].join('');
        }
        //条件查询
        function getUserByCon(){

            $("#test-table").bootstrapTable('destroy');
            $('#test-table').bootstrapTable({
                method : 'GET', //默认是post,不允许对静态文件访问
                url: "/gong/getUserByCon.action",
                cache : false,
                striped : true,// 隔行加亮
                pagination : true, //开启分页功能    在表格底部显示分页工具栏
                pageSize : 5, //默认每页条数
                pageNumber : 1, //默认分页
                pageList : [ 10, 20, 50, 100, 200, 500 ],//分页数
                showColumns : true, //显示隐藏列
                showRefresh : false, //显示刷新按钮
                toolbar:"#toolbar",
                singleselect : true,
                minimumCountColumns: 2,// 设置最少显示列个数
                clickToSelect: true, // 单击行即可以选中
                search : false,//显示搜素表单
                silent : true, //刷新事件必须设置
                sidePagination : "server", //表示服务端请求
                columns : [  {
                    checkbox:true
                }   ,{
                    field : "ghid",
                    title : "供货商编号",
                    class : 'col-md-1',
                    align : "center",
                    valign : "middle",
                    sortable : "true"
                }, {
                    field : "ghuo",
                    title : "供货商名称",
                    align : "center",
                    valign : "middle",
                    sortable : "true"
                }, {
                    field : "ghxm",
                    title : "联系人",
                    align : "center",
                    valign : "middle",
                    sortable : "true"
                },  {
                    field : "ghphone",
                    title : "电话",
                    align : "center",
                    valign : "middle",
                    sortable : "true"
                },{
                    field : "ghdizhi",
                    title : "地址",
                    align : "center",
                    valign : "middle",
                    sortable : "true"
                },
                    {
                        field : "ghbeizhu",
                        title : "备注",
                        align : "center",
                        valign : "middle",
                        sortable : "true"
                    },{
                        field: 'operate',
                        title: '操作',
                        class : 'col-md-2',
                        align: 'center',
                        valign: 'middle',
                        formatter: operateFormatter,
                    }],
                queryParamsType: "undefined",
                queryParams: function queryParams(params) {   //设置查询参数
                    var param = {
                        pageNumber: params.pageNumber,
                        pageSize: params.pageSize,
                        ghid:$("#ghid").val(),
                        ghxm:$("#ghxm").val(),
                        deptId:$("#deptId").val()
                        // searchText: params.searchText
                    };
                    return param;
                },
                formatLoadingMessage : function() {
                    return "请稍等，正在加载中...";
                },

                formatNoMatches : function() {
                    return '无符合条件的记录';
                }
            });
        }


        //查询所有部门 追加到 条件查询中
        function getDeptLists(){
            $.ajax({
                url:'/dept/getDeptList.action',
                dataType:'json',
                type:'post',
                success:function(data){
                    $("#deptId").empty();
                    $("#deptId").append("<option value='0'>请选择所属部门</option>");
                    $.each(data,function(){
                        $("#deptId").append("<option value='"+this.deptId+"'>"+this.dept_name+"</option>");
                    });
                },

            });
        }

        //导出
        function ExportExcel(){
            if(confirm("您确定要导出所有用户信息吗？")){
                location.href="/gong/export.action";
            }else{
                return false;
            }
        }



        //批量删除
        function delMany(){
            var idstr=[];
            //获取选中的行
            var a= $('#test-table').bootstrapTable('getSelections');
            if(a.length>0){
                if(confirm("您确定要删除选中的数据吗？")){
                    $.each(a,function(index){
                        idstr.push(a[index].ghid);
                    });
                    $.ajax({
                        url:'/gong/delUsers.action',
                        dataType:'json',
                        type:'post',
                        traditional:true,
                        data:{
                            ids:idstr
                        },
                        success:function(data){
                            if(data>0){
                                alert("删除成功！");
                            }else{
                                alert("删除失败！");
                            }
                            $('#test-table').bootstrapTable('refresh');
                        },
                        error:function(){
                            alert("请求失败！");
                        }
                    });
                }else{
                    return false;
                }
            }else{
                alert("请至少选中一行数据");
            }
        }
        //修改前，打开模态框
        function getValue(id){
            $.ajax({
                url:'user/getUserById.action',
                dataType:'json',
                type:'post',
                data:{
                    uid:id
                },
                success:function(data){
                    $("#ghid1").val(data.gong.ghid);
                    $("#ghuo1").val(data.gong.ghuo);
                    $("#ghxm").val(data.gong.ghxm);
                    $("#ghdizhi").val(data.gong.ghdizhi);
                    $("#ghbeizhu").val(data.gong.ghbeizhu);
                    $("#ghphone").val(data.gong.ghphone);


                    $("#mydlg1").modal('show');
                },

            });
        }


        //删除员工
        function delUser(id){
            if(confirm("您确定要删除这条数据吗?")){
                $.ajax({
                    url:'/gong/delUser.action',
                    dataType:'json',
                    type:'post',
                    data:{uid:id},
                    success:function(data){
                        if(data>0){
                            alert("删除成功！");
                        }else{
                            alert("删除失败！");
                        }
                        $("#test-table").bootstrapTable('refresh');
                    },
                    error:function(){
                        alert("请求失败！");
                    }
                });
            }
        }
        //添加，打开模态框
        function openDlg(){
            getDeptList();
            $("#mydlg").modal('show');
        };

        //关闭模态框
        function closeDlg(){
            $("#importDlg").modal('hide');
            $("#roleDlg").modal('hide');
            $("#mydlg1").modal('hide');
            $("#mydlg").modal('hide');
            $("#mid").text(null);
            $("input[type=reset]").trigger("click");
            $('#importForm').data('bootstrapValidator', null);
            $('#myform').data('bootstrapValidator', null);
            $('#myform1').data('bootstrapValidator', null);
            formValidator();
        };

        //添加用户
        function saveUser(){
            var msg=$("#mid").text();
            if(msg=="此供货商名可用"){
                if($("#myform").data('bootstrapValidator').validate().isValid()){
                    $.ajax({
                        url:'/gong/saveUser.action',
                        type:'post',
                        dataType:'json',
                        data:$("#myform").serialize(),
                        success:function(data){
                            if(data>0){
                                alert("添加成功！");
                            }else{
                                alert("添加失败！");
                            }
                            $("#test-table").bootstrapTable('refresh');
                            closeDlg();
                        },

                    });
                }else{
                    alert("无效增加!");
                }
            }else{
                alert("无效增加!");
            }
        }

        //添加用户时验证登录账号是否唯一
        function validAccount(){
            var x=$("#ghuo").val();
            if(x!=""){
                $.ajax({
                    url:'/gong/validAccount.action',
                    dataType:'json',
                    type:'post',
                    data:{
                        account:$("#ghuo").val()
                    },
                    success:function(data){
                        $("#mid").text(data.msg);
                    },

                });
            }else{
                return false;
            }
        };



        //查询所有部门
        function getDeptList(){
            $.ajax({
                url:'/dept/getDeptList.action',
                dataType:'json',
                type:'post',
                success:function(data){
                    $("#sid").empty();
                    $("#sid").append("<option value='0'>请选择</option>");
                    $.each(data,function(){
                        $("#sid").append("<option value='"+this.dept_id+"'>"+this.dept_name+"</option>");
                    });
                },

            });
        }


        $(document).ready(function(){
            formValidator();
        });
        function formValidator(){
            $("#myform").bootstrapValidator({
                fields:{
                    ghuo:{
                        validators:{
                            notEmpty:{
                                message:"供货商名不能为空"
                            },
                            stringLength:{
                                max:20,
                                message:"字符长度不能超过20个字符"
                            }
                        }
                    },
                    ghxm:{
                        validators:{
                            notEmpty:{
                                message:'姓名不能为空'
                            },
                            stringLength:{
                                max:6,
                                message:'字符长度不能超过6个字符'
                            }
                        }
                    },


                    ghphone:{
                        validators:{
                            notEmpty:{
                                message:'电话不能为空'
                            },
                            digits:{
                                message: '该值只能包含数字'
                            },
                            stringLength:{
                                min:8,
                                max:11,
                                message:'长度在8~11之间'
                            }
                        }
                    },
                    ghbeizhu:{
                        validators:{
                            notEmpty:{
                                message:"备注不能为空"
                            }
                        }
                    },

                    ghdizhi:{
                        validators:{
                            notEmpty:{
                                message:'地址不能为空'
                            },
                            stringLength:{
                                max:200,
                                message:"字符长度不能超过200"
                            }
                        }
                    }
                }
            });


            $("#myform1").bootstrapValidator({
                fields:{
                    ghid:{
                        validators:{
                            notEmpty:{
                                message:'用户编号不能为空'
                            },
                        }
                    },
                    ghuo:{
                        validators:{
                            notEmpty:{
                                message:"供货商名不能为空"
                            },
                            stringLength:{
                                max:20,
                                message:"字符长度不能超过20个字符"
                            }
                        }
                    },
                    ghxm:{
                        validators:{
                            notEmpty:{
                                message:'姓名不能为空'
                            },
                            stringLength:{
                                max:20,
                                message:'字符长度不能超过20个字符'
                            }
                        }
                    },


                    ghphone:{
                        validators:{
                            notEmpty:{
                                message:'电话不能为空'
                            },
                            digits:{
                                message: '该值只能包含数字'
                            },
                            stringLength:{
                                min:8,
                                max:11,
                                message:'长度在8~11之间'
                            }
                        }
                    },
                    ghbeizhu:{
                        validators:{
                            notEmpty:{
                                message:"备注不能为空"
                            }
                        }
                    },

                    ghdizhi:{
                        validators:{
                            notEmpty:{
                                message:'地址不能为空'
                            },
                            stringLength:{
                                max:200,
                                message:"字符长度不能超过200"
                            }
                        }
                    }
                }
            });

        }



    </script>
</head>
<body>
<br/><br/><br/>

<table id="test-table" class="table table-hover table-striped table-condensed table-bordered"></table>

<!--toolbar  -->
<div id="toolbar" class="btn-toolbar">
    <button onclick="openDlg()" type="button" class="btn btn-primary">
        <span class="glyphicon glyphicon-plus" aria-hidden="true" >添加</span>
    </button>
    <button onclick="ExportExcel()" type="button" class="btn btn-success">
        <span class="glyphicon glyphicon-paste" aria-hidden="true" >导出</span>
    </button>
    <button  type="button" onclick="delMany()" class="btn btn-danger">
        <span class="glyphicon glyphicon-trash" aria-hidden="true">删除</span>
    </button>
</div>



<!-- 模态框（Modal） -->
<!-- 添加用户 -->
<div id="mydlg" class="modal fade" id="myModal" tabindex="-1" role="dialog" data-backdrop="static" data-keyboard="false" aria-labelledby="myModalLabel" aria-hidden="true">

    <div class="modal-dialog">
        <div class="modal-content">

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">添加用户</h4>
            </div>
            <div class="container">
                <form class="form-horizontal" id="myform"  method="post">
                    <div class="form-group">
                        <label class="col-md-2 control-label">供货商名称：</label>
                        <div class="col-md-3 ">
                            <input type="text" onblur="validAccount()" id="ghuo" name="ghuo" class="form-control form-control-static" placeholder="请输入供货商名称">
                        </div>
                        <label class="control-label"><span id="mid" style="color:red"></span></label>
                    </div>

                    <div class="form-group">
                        <label class="col-md-2 control-label">联系人：</label>
                        <div class="col-md-3 ">
                            <input type="text" id="ghxm"  name="ghxm" class="form-control form-control-static" placeholder="请输入联系人">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-md-2 control-label">电话：</label>
                        <div class="col-md-3 ">
                            <input type="text" id="ghphone" name="ghphone" class="form-control form-control-static" placeholder="请输入联系电话">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-2 control-label">地址：</label>
                        <div class="col-md-3">
                            <textarea rows="1" id="ghdizhi" name="ghdizhi" cols="32" class="form-control form-control-static" placeholder="请输入地址"></textarea>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-md-2 control-label">备注：</label>
                        <div class="col-md-3 ">
                            <textarea rows="3" id="ghbeizhu" name="ghbeizhu" class="form-control form-control-static" placeholder="请输入备注"></textarea>
                        </div>
                    </div>




                    <div class="modal-footer col-md-6">
                        <!--用来清空表单数据-->
                        <input type="reset" name="reset" style="display: none;" />
                        <button type="button" class="btn btn-default" onclick="closeDlg()">关闭</button>
                        <button type="button" onclick="saveUser()" class="btn btn-primary">提交</button>
                    </div>
                </form>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

</body>
</html>